<template>
    <div>
        <el-row :gutter="20">
            <el-col :span="8">
                <el-card shadow="hover" class="mgb20" style="height: 252px">
                    <div class="user-info">
                        <img src="../../../assets/img/user_info.jpeg" class="user-avator" alt />
                        <div class="user-info-cont">
                            <div class="user-info-name">{{ name }}</div>
                            <div style="font-size: 20px; font-weight: 600; color: blue">{{ role }}</div>
                        </div>
                    </div>
                    <div class="user-info-list">
                        当前时间：<span style="font-size: 20px; font-weight: 500; color: black">{{ date | formatDate }}</span>
                    </div>
                    <div class="user-info-list">
                        当前登录地点：
                        <span style="font-size: 20px; font-weight: 500; color: black">{{ LocationCity }}</span>
                    </div>
                </el-card>
                <!-- 柱状图 -->
                <el-card shadow="hover" style="height: 400px">
                    <schart ref="bar" class="schart" canvasId="bar" :options="options"></schart>
                </el-card>
            </el-col>
            <el-col :span="16">
                <el-row :gutter="20" class="mgb20">
                    <el-col :span="6">
                        <el-card shadow="hover" :body-style="{ padding: '0px' }">
                            <div class="grid-content grid-con-1">
                                <i class="el-icon-goods grid-con-icon"></i>
                                <div class="grid-cont-right">
                                    <div class="grid-num">{{ total_packages }}</div>
                                    <div>全部包裹</div>
                                </div>
                            </div>
                        </el-card>
                    </el-col>
                    <el-col :span="6">
                        <el-card shadow="hover" :body-style="{ padding: '0px' }">
                            <div class="grid-content grid-con-2">
                                <i class="el-icon-box grid-con-icon"></i>
                                <div class="grid-cont-right">
                                    <div class="grid-num">{{ waiting_packages }}</div>
                                    <div>待取包裹</div>
                                </div>
                            </div>
                        </el-card>
                    </el-col>
                    <el-col :span="6">
                        <el-card shadow="hover" :body-style="{ padding: '0px' }">
                            <div class="grid-content grid-con-3">
                                <i class="el-icon-sold-out grid-con-icon"></i>
                                <div class="grid-cont-right">
                                    <div class="grid-num">{{ refused_packages }}</div>
                                    <div>拒签包裹</div>
                                </div>
                            </div>
                        </el-card>
                    </el-col>
                    <el-col :span="6">
                        <el-card shadow="hover" :body-style="{ padding: '0px' }">
                            <div class="grid-content grid-con-4">
                                <i class="el-icon-lx-goods grid-con-icon"></i>
                                <div class="grid-cont-right">
                                    <div class="grid-num">{{ finish_packages }}</div>
                                    <div>完成包裹</div>
                                </div>
                            </div>
                        </el-card>
                    </el-col>
                </el-row>
                <el-card shadow="hover" style="height: 700px">
                    <div slot="header" class="clearfix">
                        <el-calendar v-model="value"> </el-calendar>
                    </div>
                </el-card>
            </el-col>
        </el-row>
        <el-row :gutter="20"> </el-row>
    </div>
</template>

<script>
import Schart from 'vue-schart';
import MapLoader from '../../../AMap';
var padDate = function (value) {
    return value < 10 ? '0' + value : value;
};
export default {
    name: 'dashboard',
    data() {
        return {
            finish_packages: 0,
            total_packages: 0,
            refused_packages: 0,
            waiting_packages: 0,
            value: new Date(),
            LocationCity: '',
            date: new Date(),
            name: localStorage.getItem('user_name'),
            options: {
                type: 'bar',
                title: {
                    text: '最近一年我的快递汇总'
                },
                xRorate: 5,
                yRorate: 5,
                labels: ['第一季度', '第二季度', '第三季度', '第四季度'],
                datasets: [
                    {
                        label: '全部快递',
                        data: []
                    },
                    {
                        label: '已出库',
                        data: []
                    },

                    {
                        label: '已寄出',
                        data: []
                    },
                    {
                        label: '已拒签',
                        data: []
                    }
                ]
            }
        };
    },
    components: {
        Schart
    },
    computed: {
        role() {
            return this.name === 'admin' ? '超级管理员' : '普通用户';
        }
    },
    created() {
        MapLoader().then(
            (AMap) => {
                AMap.plugin('AMap.CitySearch', () => {
                    const citysearch = new AMap.CitySearch(); // 自动获取用户IP，返回当前城市
                    citysearch.getLocalCity((status, result) => {
                        if (status === 'complete' && result.info === 'OK') {
                            if (result && result.city && result.bounds) {
                                this.LocationCity = result.city;
                                console.log('当前城市', this.LocationCity); // var citybounds = result.bounds; // 地图显示当前城市 // map.setBounds(citybounds);
                            }
                        } else {
                            console.log('定位出错');
                        }
                    });
                });
            },
            (err) => {
                console.log('地图加载失败', err);
            }
        );

        let param = new URLSearchParams();
        param.append('to_name', localStorage.getItem('to_name'));
        const _this = this;
        this.$axios({
            method: 'POST',
            url: 'statistics/statistics_packages',
            data: param,
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' } //加上这个
        })
            .then(function (res) {
                if (res.data.code == 1) {
                    _this.finish_packages = res.data.finishedPackages;
                    _this.total_packages = res.data.totalPackages;
                    _this.refused_packages = res.data.refusesPackages;
                    _this.waiting_packages = res.data.waitingPackages;
                }
            })
            .catch(function (err) {
                console.log(err);
            });
        this.$axios({
            method: 'POST',
            url: 'statistics/statistics_packages_by_quarter',
            data: param,
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' } //加上这个
        })
            .then(function (res) {
                if (res.data.code == 1) {
                    console.log(res);
                    _this.options.datasets[0].data = res.data.totalPackagesArray;
                    _this.options.datasets[1].data = res.data.exportedPackagesArray;
                    _this.options.datasets[2].data = res.data.sentPackagesArray;
                    _this.options.datasets[3].data = res.data.refusesPackagesArray;
                }
            })
            .catch(function (err) {
                console.log(err);
            });
    },

    methods: {},
    filters: {
        formatDate: function (value) {
            var date = new Date(value);
            console.log(date.getDate());
            var year = date.getFullYear();
            var month = padDate(date.getMonth() + 1);
            var day = padDate(date.getDate());
            var hours = padDate(date.getHours());
            var minutes = padDate(date.getMinutes());
            var seconds = padDate(date.getSeconds());
            // 整理并返回
            return year + '年' + month + '月' + day + '日' + ' ' + hours + ':' + minutes + ':' + seconds;
        }
    },
    mounted() {
        let _this = this; // 声明一个变量指向Vue实例this，保证作用域一致
        this.timer = setInterval(() => {
            _this.date = new Date(); // 修改数据date
        }, 1000);
    },
    beforeDestroy() {
        if (this.timer) {
            clearInterval(this.timer); // 在Vue实例销毁前，清除我们的定时器
        }
    }
};
</script>


<style scoped>
.el-row {
    margin-bottom: 20px;
}

.grid-content {
    display: flex;
    align-items: center;
    height: 100px;
}

.grid-cont-right {
    flex: 1;
    text-align: center;
    font-size: 14px;
    color: #999;
}

.grid-num {
    font-size: 30px;
    font-weight: bold;
}

.grid-con-icon {
    font-size: 50px;
    width: 100px;
    height: 100px;
    text-align: center;
    line-height: 100px;
    color: #fff;
}
.grid-con-1 .grid-con-icon {
    background: rgb(45, 140, 240);
}
.grid-con-1 .grid-num {
    color: rgb(45, 140, 240);
}
.grid-con-2 .grid-con-icon {
    background: rgb(100, 213, 114);
}
.grid-con-2 .grid-num {
    color: rgb(45, 140, 240);
}
.grid-con-3 .grid-con-icon {
    background: rgb(242, 94, 67);
}
.grid-con-3 .grid-num {
    color: rgb(242, 94, 67);
}
.grid-con-4 .grid-con-icon {
    background: rgb(242, 9, 237);
}
.grid-con-4 .grid-num {
    color: rgb(242, 9, 237);
}
.user-info {
    display: flex;
    align-items: center;
    padding-bottom: 20px;
    border-bottom: 2px solid #ccc;
    margin-bottom: 20px;
}

.user-avator {
    width: 120px;
    height: 120px;
    border-radius: 50%;
}

.user-info-cont {
    padding-left: 50px;
    flex: 1;
    font-size: 14px;
    color: #999;
}

.user-info-cont div:first-child {
    font-size: 30px;
    color: #222;
}

.user-info-list {
    font-size: 14px;
    color: #999;
    line-height: 25px;
    /* font-size: 20px; */
    font-weight: 500;
    color: black;
}

.user-info-list span {
    margin-left: 70px;
}

.mgb20 {
    margin-bottom: 20px;
}

.todo-item {
    font-size: 14px;
}

.todo-item-del {
    text-decoration: line-through;
    color: #999;
}

.schart {
    width: 100%;
    height: 300px;
}
</style>
